package com.probiz.estore.common.model.customer;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.base.CustomerTbl;
import com.probiz.estore.common.service.CustomerService;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.other.service.TagManager;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * Model - Business object
 * This file won't get overwritten.
 */
public class Customer extends CustomerTbl {
	
	
	/**
	 * 标准参考写法
	 * 
	 * 规范：
	 * 1. 命名(FilterHqlName)按类名规则命名，里面的实例(COUNT_ACTIVE)按常量命名方式
	 * Tip：ctrl+shift+x 小写转大写
	 * 
	 * @since V3.5
	 */
	public static enum FilterHqlName{COUNT_BY_MEMBERSHIP_ID, COUNT_CUSTOMER_TOTAL};
	
	private String rePassword;//前台注册验证用
  
	/**
	 * 获得客户地域地理位置
	 * @return
	 */
	public String getCustomerArea(){
		String area = "";
		if (StringUtils.isNotBlank(this.getAddress())) {
			String[] addresses = this.getAddress().split(" ");
			area = addresses[0];
		}
		return area;
	}
	/**
	 * Default Empty Constructor for class Customer
	 */
	public Customer () {
		super();
	}
	
	/**
	 * Default Key Fields Constructor for class Customer
	 */
	public Customer (
		 Integer in_customerId
		) {
		super (
		  in_customerId
		);
	}
	
	public String getJsonObject(){
		JSONObject json = new JSONObject();
		json.put("appuserId", appuserId);
		json.put("username", username);
		json.put("email", email);
		json.put("fullName", super.getFullName());
		
		return json.toString();
	}
	public JSONObject getCustomerJsonObject(){
		JSONObject json = new JSONObject();
		json.put("customerId", appuserId);
		json.put("username", username);
		json.put("firstname", firstname);
		json.put("telephone", telephone);		
		json.put("email", email);
		json.put("fullName", super.getFullName());
		Address defaultAddress = this.getDefaultAddress();
		if (defaultAddress != null) {
			json.put("address", defaultAddress.getAddress());
			json.put("zip", defaultAddress.getZip());
			json.put("regionId", defaultAddress.getRegionId());
			json.put("regionFullName", defaultAddress.getRegionFullName());
			json.put("telephone", defaultAddress.getTelephone());
		}else{
			json.put("address", "");
			json.put("zip", "");
			json.put("regionId", Constants.REGIONID_CHINA);
			json.put("regionFullName", "");
			json.put("telephone", "");
			
		}
		return json;
	}
		
	/**
     * 获得标签名称
     * @return
     */
    public String getTagNames(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNames(this);
    }
    /**
     * 获得标签数组
     * @return
     */
    public String[] getTagNameArray(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNameArray(this);
    }
    
    public boolean isAnonymousUser() {
    	if (super.getCustomerId() != null && super.getCustomerId() > 0) {
    		return false;
    	} else {
    		return true;
    	}
	}
    
    public Address getDefaultAddress() {
    	Address address = getCustomerService().getDefShippingAddr(getAppuserId());
    	return address;
    }
	
	private CustomerService getCustomerService() {
		return (CustomerService)RequestContext.getSpringBeanById("customerService");
	}
	public String getRePassword() {
		return rePassword;
	}
	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
	
}
